package com.weavernorth.action.salaryXJ;

import com.weavernorth.util.Logger4jUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.Logger;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.math.BigDecimal;

/**
 * 此action是店铺销售提成对比月任务,正价销售汇总,折价销售 汇总,给出提示
 */
public class xjShopSalarControlAction extends BaseBean implements Action{

//	public static Log log = LogFactory.getLog(xjShopSalarControlAction.class.getName());
private Logger log= Logger4jUtil.getLoggerByName("/action/api/xjShopSalarControlAction/");
	/**
	 * 终端店铺销售提成申请表 第一节点
	 */
	@Override
	public String execute(RequestInfo request) {
		//数据操作
		RecordSet rs=new RecordSet();
		//当前表
		String strTable=request.getRequestManager().getBillTableName();
		//当前请求id
		String strRequestid= Util.null2String(request.getRequestid());

		//主表月任务
		Double douMainYrw=0.0;
		//明细表月任务总和
		BigDecimal douYrwSum=new BigDecimal("0.0");

		//主表店铺销售总流水
		BigDecimal dpzxslsMain=new BigDecimal("0.0");
		//明细表销售总流水总和
		BigDecimal xszlsSum=new BigDecimal("0.0");

		//主表店铺小程序正价流水
		BigDecimal xcxzjlsMain=new BigDecimal("0.0");
		//明细表小程序正价流水总和
		BigDecimal 	xcxzjzlsSum=new BigDecimal("0.0");

		//主表店铺小程序折价流水
		BigDecimal xcxzhejlsMain=new BigDecimal("0.0");
		//明细表小程序折价流水总和
		BigDecimal xcxzhejzlsSum=new BigDecimal("0.0");
		BigDecimal defaultNum=new BigDecimal("0.0");
		log.info("新疆-终端店铺销售提成校验开始.........");
		//查询当前表数据
		String sql="select * from "+strTable+" fm,"+strTable+"_dt1 fmd where fm.id=fmd.mainid and fm.requestid=? and (fmd.sfdqdrw='0' or fmd.sfdqdrw is null)";
         log.info("查询店铺销售提成信息:"+sql+";requestid:"+strRequestid);
         rs.executeQuery(sql,strRequestid);
         try{
         while (rs.next()){
         	//主表月任务
			 douMainYrw=Util.getDoubleValue(rs.getString("yrwhz"),0.0);
			 log.info("1.主表月任务："+douMainYrw);
         	 //明细表销售人员的修改后的月任务
			 BigDecimal douYrw=new BigDecimal(Util.null2String(rs.getString("yrw"),"0.0"));
			 log.info("2.子表月任务："+douYrw);
			 douYrwSum=douYrwSum.add(douYrw);
			 log.info("3.子表月任务累加："+douYrwSum);
			 //主表总销售流水
			 if(dpzxslsMain.compareTo(defaultNum)==0){
				 log.info("判断总销售流水是否为0：");
				 dpzxslsMain=new BigDecimal(convertString(rs.getString("dpzxsls")));
			 }
			 log.info("4.主表总销售流水："+dpzxslsMain);
			 //明细表销售总流水的修改后的值
			 BigDecimal xszls=new BigDecimal(convertString(rs.getString("xszls1")));
			 log.info("5.明细表销售总流水的修改后的值："+xszls);
			 xszlsSum=xszlsSum.add(xszls);
			 log.info("6.明细表销售总流水累加："+xszlsSum);
			 //主表店铺小程序正价流水
			 if (xcxzjlsMain.compareTo(xcxzjlsMain)==0){
				 log.info("判断主表店铺小程序正价流水是否为0");
				 xcxzjlsMain=new BigDecimal(convertString(rs.getString("xcxzjls")));
			 }
			 log.info("7.主表店铺小程序正价流水："+xcxzjlsMain);
			 //明细表小程序正价流水的修改后的值
			 BigDecimal xcxzjzls=new BigDecimal(convertString(rs.getString("xcxzjzls")));
			 log.info("8.明细表小程序正价流水的修改后的值："+xcxzjzls);
			 xcxzjzlsSum=xcxzjzlsSum.add(xcxzjzls);
			 //主表店铺小程序折价流水
			 if (xcxzhejlsMain.compareTo(xcxzhejlsMain)==0) {
				 xcxzhejlsMain = new BigDecimal(convertString(rs.getString("xcxzhejls")));
			 }
			 log.info("9.主表店铺小程序折价流水："+xcxzhejlsMain);
			 //明细表小程序折价流水的修改后的值
			 BigDecimal xcxzhejzls=new BigDecimal(convertString(rs.getString("xcxzhejzls")));
			 log.info("10.明细表小程序折价流水的修改后的值："+xcxzhejzls);
			 xcxzhejzlsSum=xcxzhejzlsSum.add(xcxzhejzls);
			 log.info("11.明细表小程序折价流水累加："+xcxzhejzlsSum);
		 }
		 }catch (Exception ex){
         	ex.printStackTrace();
			 log.info("异常信息："+ex.toString());
			 request.getRequestManager().setMessagecontent("异常信息："+ex.toString());
			 return FAILURE_AND_CONTINUE;
		 }
		 log.info("月任务,店铺:"+douMainYrw+",总和:"+douYrwSum);
		 //销售人员的修改后的月任务必须等于店铺总任务
		 if(douYrwSum.doubleValue()!=douMainYrw){
         	request.getRequestManager().setMessagecontent("销售人员调整后的月任务必须等于店铺总月任务");
         	return FAILURE_AND_CONTINUE;
		 }
		log.info("销售流水,店铺:"+dpzxslsMain+",明细总和:"+xszlsSum);
		//销售人员的修改后的销售总流水不可大于店铺总销售流水
		//xszlsSum.doubleValue()>dpzxslsMain
		if(compare(xszlsSum,dpzxslsMain)){
			request.getRequestManager().setMessagecontent("销售人员调整后的销售总流水总和不可大于店铺总销售流水");
			return FAILURE_AND_CONTINUE;
		}
		log.info("小程序正价流水,店铺:"+xcxzjlsMain+",明细总和:"+xcxzjzlsSum);
		//明细表小程序正价总流水之和不能大于店铺小程序正价流水xcxzjzlsSum.doubleValue()>xcxzjlsMain
		if(compare(xcxzjzlsSum,xcxzjlsMain)){
			request.getRequestManager().setMessagecontent("销售人员调整后的小程序正价总流水总和不可大于店铺小程序正价流水");
			return FAILURE_AND_CONTINUE;
		}
		log.info("小程序折价流水,店铺:"+xcxzhejlsMain+",明细总和:"+xcxzhejzlsSum);
		//明细表小程序折价总流水之和不能大于店铺小程序折价流水xcxzhejzlsSum.doubleValue()>xcxzhejlsMain
		if(compare(xcxzhejzlsSum,xcxzhejlsMain)){
			request.getRequestManager().setMessagecontent("销售人员调整后的小程序折价总流水总和不可大于店铺小程序折价流水");
			return FAILURE_AND_CONTINUE;
		}
	  return SUCCESS;
	}
	public Boolean compare(BigDecimal val1, BigDecimal val2) {
		Boolean result = true;
		if (val1.compareTo(val2) < 0) {
			result=false;//val2>val1
		}
		if (val1.compareTo(val2) == 0) {
			result=false;//val2=val1
		}
		if (val1.compareTo(val2) > 0) {
			result=true;
		}
		return result;
	}
	public  String convertString(String strVal){
		String strResult= Util.null2String(strVal);
		if (strResult==""){
			strResult="0.0";
		}
		return strResult;
	}
}
